<!--  -->
<template>
  <div class="pwdLogin-container">
    <!-- 头部 -->
    <van-nav-bar title="登录" fixed :z-index="100" @click-left="onBack">
      <template #left>
        <van-icon
          name="arrow-left"
          size="20"
          color="#fff"
          @click="$router.go(-1)"
          slot="left"
        />
      </template>
    </van-nav-bar>
    <div style="margin-top: 30px"></div>
    <van-form style="margin-top: 13px">
      <van-field
        v-model="usercode"
        :state="nameState"
        @blur.native.capture="checkName"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="pwdword"
        :state="pwdState"
        @blur.native.capture="checkPwd"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px" class="btnn">
        <van-button
          @click="checkForm"
          round
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      usercode: "", // 标识用户名输入框的值
      nameState: "", // 标识用户名输入框的状态
      pwdword: "",
      pwdState: "",
      //校验规则
      rules: {
        name: {
          reg: /^1[3456789]\d{9}$/,
          toast: "请输入11位合法的手机号",
        },
        pwd: {
          reg: /^\w{6,16}$/,
          toast: "密码必须为6到16位数字，字母，下划线组成",
        },
      },
    };
  },

  methods: {
    // ...mapMutations(["saveUserInfo"]),
    //返回首页
    onBack() {
      this.$router.push({ name: "Home" });
    },
    //去注册页面
    toRegister() {
      this.$router.push({ name: "Zhuce" });
    },

    /** 验证用户名 */
    checkName() {
      let exp = /^\w{6,15}$/;
      if (exp.test(this.usercode)) {
        // 验证成功
        this.nameState = "success";
        return true;
      } else {
        // 验证失败
        this.nameState = "error";
        return false;
      }
    },

    /** 验证密码框 */
    checkPwd() {
      let exp = /^\d{6}$/;
      if (exp.test(this.pwdword)) {
        this.pwdState = "success";
        return true;
      } else {
        this.pwdState = "error";
        return false;
      }
    },

    /** 验证表单 */
    checkForm() {
      if (this.checkName() && this.checkPwd()) {
        console.log("验证成功,开始发送登录请求...");
        this.axios
          .post(
            "http://172.88.22.110:3000/mumber/login",
            `usercode=${this.usercode}&password=${this.pwdword}`
          )
          .then((res) => {
            if (res.data.code == 200) {
              // 登录成功
              // 提交vuex，更新用户信息
              this.$store.commit("loginok", this.usercode);

              // 弹窗跳转
              this.$toast("登录成功");
              this.$router.push("/me");
            } else {
              // 登录失败
              this.$messagebox({
                title: "提示",
                message: "账号密码输入错 误,请重试",
              });
            }
          });
      }
    },
  },
};
</script>

<style scoped>
.pwdLogin-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f5f5;
}
.pwdLogin-container >>> .van-nav-bar__content {
  background-color: rgb(25, 137, 250);
}
.pwdLogin-container >>> .van-nav-bar__title {
  color: #fff;
}
.content {
  flex: 1;
  padding-top: 46px;
}
.form-wrapper {
  padding: 20px;
}
.user-tel,
.user-pwd {
  display: flex;
  align-items: center;
  height: 44px;
  background-color: #fff;
  margin-bottom: 20px;
  border: 1px solid #ccc;
}
.form-wrapper input {
  font-size: 14px;
  padding-left: 10px;
}
.form-wrapper .login-btn {
  height: 44px;
  margin-bottom: 20px;
  color: #fff;
  text-align: center;
  line-height: 44px;
  background-color: #b64f4a;
}
.relevant-links {
  display: flex;
  font-size: 14px;
}
.relevant-links > div {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
}
.relevant-links > div span {
  margin-left: 4px;
}
</style>
